package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.DictData;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.DictDataMapper;
import com.sixth.system.mapper.DictTypeMapper;
import com.sixth.system.service.DictDataService;
import com.sixth.system.vo.DictDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/14 15:48
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate; //key value
    @Override
    public R<List<DictData>> selectByType(String type) {
        //1.查询缓存是否存在
        Object o = redisTemplate.opsForValue().get("dict_data::" + type);
        if(o!=null){
            List<DictData> dictData = (List<DictData>) o;
            return R.ok(dictData);
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("dict_type",type);
        List<DictData> dictData = dictDataMapper.selectList(wrapper);
        if(dictData.size()>0){
            redisTemplate.opsForValue().set("dict_data::"+type,dictData,12, TimeUnit.HOURS);
        }
        return R.ok(dictData);
    }

    @Override
    public R<Page<DictData>> selectByPage(DictDataVo dictDataVo) {
        Page<DictData> page = new Page<>(dictDataVo.getPageNum(), dictDataVo.getPageSize());
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        if (dictDataVo.getStatus()!=null){
            wrapper.eq("status",dictDataVo.getStatus());
        }
        Page<DictData> dictDataPage = dictDataMapper.selectPage(page, wrapper);
        return R.ok(dictDataPage);
    }

    @Override
    public R<DictData> getByDictId(Long dictCode) {
        DictData dictData = dictDataMapper.selectById(dictCode);
        return R.ok(dictData);
    }
    @Override
    public R<String> getDictTypeById(Long dictId) {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+dictId);
        System.out.println(dictTypeMapper.selectById(dictId).getDictType());
        return R.ok(dictTypeMapper.selectById(dictId).getDictType());
    }




    @Override
    public R<Page<DictData>> listByDataType(DictDataVo dictDataVo) {
        Page<DictData> page = new Page<>(dictDataVo.getPageNum(), dictDataVo.getPageSize());
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        if (dictDataVo.getStatus()!=null){
            wrapper.eq("status",dictDataVo.getStatus());
        }
        wrapper.orderByAsc("dict_sort");
        wrapper.eq("dict_type",dictDataVo.getDictType());
        Page<DictData> dictDataPage = dictDataMapper.selectPage(page, wrapper);
        return R.ok(dictDataPage);
    }

    @Override
    public boolean addUpdate(DictData dictData) {
        dictData.setCreateTime(new java.util.Date());
        dictData.setDictType(dictData.getDictType());
        boolean res = dictDataMapper.insertOrUpdate(dictData);
        return res;
    }

    @Override
    /**
     * 批量删除字典数据
     *
     * @param dictIds 字典数据的ID数组，用于标识需要删除的具体数据
     * @return 操作结果，R对象封装了操作状态和可能的消息
     *
     * 此方法负责批量删除指定ID的字典数据它首先将ID数组转换为字典数据对象的列表，
     * 每个对象都通过ID构造然后，调用映射器方法批量删除这些数据对象
     *
     * 为什么要转换为对象列表？因为映射器的deleteBatchIds方法需要一个实体对象列表作为参数，
     * 而不是简单的ID列表这样可以保证在数据库操作时有更准确的数据匹配和操作
     */
    public R batchDelete(Long[] dictIds) {
        // 将用户Id数组转换为对象列表，删除
        List<DictData> collect= Arrays.stream(dictIds).map(item -> {
            DictData dictData = new DictData();
            dictData.setDictCode(item);
            return dictData;
        }).collect(Collectors.toList());
        dictDataMapper.deleteBatchIds(collect);
        return R.ok();
    }

}
